package riofrio;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import riofrio.TreeNode;

/**
 * Iterator for the breadth-first walk.
 * 
 * @author Victor Hugo Herrera Maldonado
 */
class BreadthFirstWalkIterator<T> implements Iterator<TreeNode<T>> {
    private final Queue<TreeNode<T>> queue = new LinkedList<>();

    BreadthFirstWalkIterator(TreeNode<T> root) {
        queue.add(root);
    }

    @Override
    public boolean hasNext() {
        return !queue.isEmpty();
    }

    @Override
    public TreeNode<T> next() {
        TreeNode<T> node = queue.poll();
        if (node == null) {
            throw new IllegalStateException("End of walking");
        }
        for (TreeNode<T> child : node) {
            queue.add(child);
        }
        return node;
    }
    
}
